const db = require('../db/db');

//获取收藏(获取申请人id,返回被收藏者id数组)
exports.get_collected = (req,res) => {
    const get_id = req.query.post_col_id;
    var arr=[];
    const sql1 = `select get_col_id from user_collected where post_col_id=? and status=0`;
    db.query(sql1,get_id,(err,result)=>{
        if(err) return console.log(err.message);
        arr=JSON.stringify(result);
        arr= JSON.parse(arr);
        res.send(arr)
        //[{"get_col_id":28},{"get_col_id":29}]
    })
    
}
//收藏（收藏人id，被收藏人id）
exports.collected =(req,res)=>{
    const arr = req.query;
    const sqlstr = 'insert into user_collected set ?';
    db.query(sqlstr,arr,(err,result)=>{
        if(err) return console.log(err.message);
        if(result.affectedRows===1){
            res.send('插入成功');
        }
    })

}

//取消收藏(user_collected里数据id)
exports.del_collected=(req,res)=>{
    const post_col_id =req.query.post_col_id;
    const get_col_id=req.query.get_col_id;
    const sqlstr = 'update user_collected set status=1 where post_col_id =? and get_col_id=? ';
    db.query(sqlstr,[post_col_id,get_col_id],(err,result)=>{
        if(err) return console.log(err.message);
        // if(result.affectedRows === 1){
        //     res.send('取消收藏成功');
        // }
        res.send('取消收藏成功');
    })
}

//后台获取所有收藏
exports.all_c = (req,res) => {
    var arr =[];
    const sql1 = `select * from user_collected`;
    db.query(sql1,(err,result)=>{
        if(err) return console.log(err.message);
        arr=JSON.stringify(result);
        arr= JSON.parse(arr);
        res.send(arr)
        //[{"get_col_id":28},{"get_col_id":29}]
    })
    
}

//后台搜索收藏
exports.hsearch_col = (req,res) => {
    var data= req.query.id;
    var arr =[];
    const sql1 = `select * from user_collected where id=?`;
    db.query(sql1,data,(err,result)=>{
        if(err) return console.log(err.message);
        arr=JSON.stringify(result);
        arr= JSON.parse(arr);
        res.send(arr)
        //[{"get_col_id":28},{"get_col_id":29}]
    })  
}

exports.hchange_col =(req,res) => {
    const data = req.query;
    const sqlstr = 'update user_collected set status=? where id =?';
    db.query(sqlstr,[data.status,data.id],(err,result)=>{
        if(err) return console.log(err.message);
        if(result.affectedRows===1){
            res.send('信息修改成功');
        }
    })
}